package com.sunmi.usercenter.ui.activity;

import java.io.File;

import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.controller.FragmentController;
import com.sunmi.usercenter.controller.IFragmentController;
import com.sunmi.usercenter.ui.activity.fragment.LoginFragment;
import com.sunmi.usercenter.ui.activity.fragment.StartRegisterFragment;
import com.sunmi.usercenter.utils.CharUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 项目名称：Sunmi-User-Center
 * 类描述：
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/6/8 10:39
 * 修改人：longx
 * 修改时间：2016/6/8 10:39
 * 修改备注：
 */
public class HomeActivity extends SuperFragmentActivity implements IFragmentController, OnClickListener{

	private FragmentController mFragmentController;
	private View mRelStatus;


	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		initView();
		loadFragment();
    }

	@SuppressLint("NewApi")
	private void loadFragment() {
		FragmentManager fm = getSupportFragmentManager();
		mFragmentController = FragmentController.getInstance(this, fm);
//		String custom_launcher = "com.sunmi.usercenter";
		String custom_launcher = Settings.Global.getString(getApplicationContext().getContentResolver(), "custom_launcher");
		if (!CharUtils.isEmpty(custom_launcher)) {
			if (custom_launcher.equals("com.sunmi.usercenter")) {
				Constant.ME_IS_LAUNCHER = true;
//				immerseNotificationBar();
				Settings.Secure.putInt(getContentResolver(), "usercenter_show", 1);
				openFM(new FmInfo(StartRegisterFragment.class,null));
			}else{
				Constant.ME_IS_LAUNCHER = false;
				openFM(new FmInfo(LoginFragment.class,null));
			}
		} else {
			Constant.ME_IS_LAUNCHER = false;
			openFM(new FmInfo(LoginFragment.class,null));
		}
	}

	private void initView() {
		immerseNotificationBar();
		setContentView(R.layout.activity_home);
		mRelStatus =  this.findViewById(R.id.sta);
		FragmentManager fm = getSupportFragmentManager();
		mFragmentController = FragmentController.getInstance(this, fm);
	}

	private long exitTime = 0;
	
    @Override
    public void onBackPressed() {
    	switch (Constant.CLASS_FRAGMENT) {
		case Constant.DoubleClickExit:
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				exitTime = System.currentTimeMillis();
				Toast.makeText(getApplicationContext(), getResources().getText(R.string.txt_exit), Toast.LENGTH_SHORT)
						.show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
			}
			break;
			case "not_back":

				break;
		default:


//			List<Fragment> list = fm.getFragments();
//			if(list!=null&&list.size()>0){
//				for(int i=0;i<list.size();i++){
//					Fragment fragment = list.get(i);
//					LogUtil.d(TAG, "position:"+i+" name:"+fragment);
//				}
//			}
			super.onBackPressed();
//			closeFM();

			break;
		}
    	
    }

	public void SetStatusColorf5f5f5(){
		mRelStatus.setBackgroundResource(R.color.bg);
	}
	
	public void SetStatusColorFE542B(){
		mRelStatus.setBackgroundResource(R.color.CFE542B);
	}
	
	@SuppressLint({ "InlinedApi", "NewApi" })
	private void immerseNotificationBar(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
	}
	

	
	@Override
	public void openFM(FmInfo fmInfo) {
		mFragmentController.openFM(fmInfo);
	}

	@Override
	public void closeFM() {
		mFragmentController.closeFM();
	}

	@Override
	public void onResume() {
		super.onResume();
	}


	
	@Override
	public void hideSoftInput(Context context, View achor) {
		
	}

	@Override
	public void loadIMG(ImageView img, File file) {
		
	}
}
